<template>
  <div class="profile-box">
    <!-- 未登录头部 -->
    <div v-if="!user" class="not-login header-bg">
      <img src="@/assets/mobile.png" @click="$router.push('/login')" />
      <span class="text">登录/注册</span>
    </div>
    <!-- 已登录头部 -->
    <div v-else class="login-info header-bg">
      <!-- 用户资料 -->
      <div class="user-info">
        <!-- 左侧 -->
        <div class="left">
          <van-image class="avtive" round :src="userInfo.photo" />
          <span class="username">{{userInfo.name}}</span>
        </div>
        <!-- 右侧 -->
        <div class="right">
          <van-button to="/user" round size="mini" type="default">编辑资料</van-button>
        </div>
      </div>
      <!-- 用户数据 -->
      <div class="user-data">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- 宫格导航 -->
    <van-grid :column-num="2" class="grid-iten">
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="grid-text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text" class="grid-text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 底部导航 -->
    <div class="box-cell">
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
    </div>
    <van-cell v-if="user" title="退出登录" @click="logoutClick" class="logout-cell" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserProfile } from '@/api/user/index.js'
export default {
  name: 'proflie',
  data () {
    return {
      userInfo: {}
    }
  },
  methods: {
    // 退出登录逻辑
    logoutClick () {
      this.$dialog.confirm({
        title: '头条头条',
        message: '确认退出吗？'
      })
        .then(() => {
          this.$store.commit('initUser', null)
        })
        .catch(() => {
          // on cancel
        })
    },
    // 获取用户信息
    async loadUser () {
      // console.log('加载中啊')
      try {
        const res = await getUserProfile()
        // console.log(res)
        this.userInfo = res.data.data
      } catch (err) {
        this.$toast('获取失败')
      }
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {},
  created () {
    if (this.user) {
      this.loadUser()
    }
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.profile-box {
  // 公共样式
  .header-bg {
    width: 750px;
    height: 401px;
    background-image: url(~@/assets/banner.png);
    background-size: 100%;
  }
  // 未登录样式
  .not-login {
    display: flex;
    // 主轴居中
    justify-content: center;
    // 侧轴居中
    align-items: center;
    // 改变主轴方向
    flex-direction: column;
    img {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
    }
    .text {
      font-size: 28px;
      color: #fff;
    }
  }
  // 已登录样式
  .login-info {
    .user-info {
      display: flex;
      justify-content: space-between;
      align-items: flex-end;
      padding: 0 32px;
      height: 247px;
      // background-color: skyblue;
      // 左侧右侧样式
      .left,
      .right {
        display: flex;
        align-items: center;
        height: 132px;
        .avtive {
          margin-right: 22px;
          width: 132px;
          height: 132px;
        }
        .username {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    // 数据
    .user-data {
      display: flex;
      height: 154px;
      // background-color: yellow;
      .data-item {
        justify-content: center;
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        color: #fff;
        span:nth-child(1) {
          font-size: 26px;
          margin-bottom: 20px;
        }
        span:nth-child(2) {
          font-size: 22px;
        }
      }
    }
  }
  // 宫格导航
  .grid-iten {
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    .grid-text {
      font-size: 28px;
      color: #333;
      margin-top: 16px;
      letter-spacing: 10px;
    }
  }
  // 底部导航
  .box-cell {
    margin: 10px 0;
  }
  .logout-cell {
    text-align: center;
    color: #d86262;
  }
}
</style>
